package com.senven.type.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.senven.common.entity.Type;
import com.senven.common.pojo.vo.CommonResult;
import com.senven.common.pojo.vo.TypeVo;
import com.senven.type.mapper.TypeMapper;
import com.senven.type.service.TypeService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author YSH
 * @since 2022-09-01
 */
@Service
public class TypeServiceImpl extends ServiceImpl<TypeMapper, Type> implements TypeService {

    @Autowired
    private TypeMapper typeMapper;

    @Override
    public CommonResult selectType(Integer currentPage, Integer pageSize, TypeVo typeVo) {
        Page<Type> page = new Page<>(currentPage,pageSize);

        QueryWrapper<Type> wrapper = new QueryWrapper<>();

        if (StringUtils.hasText(typeVo.getName())){
            wrapper.like("name",typeVo.getName());
        }

        if (StringUtils.hasText(typeVo.getStartDate())){
            wrapper.ge("gmt_create",typeVo.getStartDate());
        }

        if (StringUtils.hasText(typeVo.getEndDate())){
            wrapper.le("gmt_create",typeVo.getEndDate());
        }

        Page<Type> typePage = typeMapper.selectPage(page, wrapper);
        System.out.println(page);

        return new CommonResult(2000,"查询成功",typePage);
    }

    @Override
    public CommonResult insertType(Type type) {
        int insert = typeMapper.insert(type);
        return new CommonResult(2000,"添加成功",insert);
    }

    @Override
    public CommonResult updateType(Type id) {
        int updateById = typeMapper.updateById(id);
        return new CommonResult(2000 ,"修改成功",updateById);
    }

    @Override
    public CommonResult delType(Integer id) {
        int deleteById = typeMapper.deleteById(id);
        return new CommonResult(2000,"删除成功",deleteById);
    }
}
